scroll_text:
	call	DISSCR
	call	scroll_text_load_ASCII

	call	init_font_buffer

	call	scrolltext_load_font
	call	scrolltext_init_colors

	call	cls_screen
	call	load_logo
	call	display_logo

	call	init_text_buffer

	xor		a
	ld		[txtscrolllin],a
	ld		[dump_step],a

	ld		hl,TEXT
	ld		de,graphics_buffer + (32*16);(32*12)
	call	unpack

	ld		hl,graphics_buffer + (32*16)
	ld		[txt_ptr],hl
	ld		a,1
	ld		[odd],a

	ld		b,8

	push	bc
	push	af
	call	ENASCR
	pop		af
	pop		bc

scrolltext_main_loop:

	ld		hl,dump_step
	inc		[hl]
	ld		a,[dump_step]
	and		00000011b
	jr		nz,no_update

	push	bc
	
	call	scroll_font_1_line

	halt

	call	scrolltext_load_font
	call	scrolltext_dump_lines
		
	pop		bc
	ld		a,b
	cp		1
	jr		nz,.newhalt
	jr		.loop_end
.newhalt:
	halt	
.loop_end:	
	djnz	scrolltext_main_loop
	//di
	
	call	scrolltext_scroll_lines
	//ei
	
	ld		hl,txtscrolllin
	ld		a,50								;number of lines to scroll
	cp		[hl]
	jr		z,scroll_text_no_more_scroll
	inc		[hl]

	ld		b,8
	
no_update:	
	halt
	
	ld		hl,KEY_SPACE
	call	get_keyboard
	and		a
	jr		z,scroll_text_another_loop
scroll_text_wait_until_space_is_released:
	halt	
	ld		hl,KEY_SPACE
	call	get_keyboard
	and		a
	ret		z
	jr		scroll_text_wait_until_space_is_released


scroll_text_another_loop:

	jr		scrolltext_main_loop

;end scroll_text


scroll_text_no_more_scroll:

	halt
	ld		hl,KEY_SPACE
	call	get_keyboard
	and		a
	jr		z,scroll_text_no_more_scroll
scroll_text_wait_until_space_is_released_2:
	halt	
	ld		hl,KEY_SPACE
	call	get_keyboard
	and		a
	ret		z
	jr		scroll_text_wait_until_space_is_released_2






init_text_buffer:
	xor		a
	ld		hl,text_buffer
	ld		[hl],a
	push	hl
	pop		de
	inc		de
	ld		bc,(32*24)-1
	ldir	

	ret

init_font_buffer:

	ld		hl,text_buffer ;;;;;;;;;;;;;;;;;;;;;;;;;;;
	ld		de,font_buffer
	ld		bc,56*8
	ldir	
	
	ret
	
scrolltext_init_colors:

	ld		a,0xF1
	ld		bc,3*256*8
	ld		hl,CLRTBL
	call	0x56
	
	
	ld		hl,CLRTBL
	ld		bc,8
	ld		a,0x11
	call	0x56

	ld		hl,CLRTBL + (256*8)
	ld		bc,8
	ld		a,0x11
	call	0x56

	ld		hl,CLRTBL + 2*(256*8)
	ld		bc,8
	ld		a,0x11
	call	0x56
		

	ret
	
	
	
	
	/*
; Adapted from MSX CBIOS	
setwrt:
                di
;        IF VDP != TMS99X8
;                xor     a
;               out     (VDP_ADDR),a
;               ld      a,128+14
;               out     (VDP_ADDR),a
;        ENDIF

                ld      a,l
                out     (VDP_ADDR),a
                ld      a,h
                and     $3F
                or      $40
                out     (VDP_ADDR),a
                ei

        ret	
*/

; Adapted from MSX CBIOS
myldirvm:
                ex      de,hl
;        IF VDP = TMS99X8
                //call    setwrt
				;///////////////////////////

				di
                ld      a,l
                out     (VDP_ADDR),a
                ld      a,h
                and     $3F
                or      $40
                out     (VDP_ADDR),a
                ei                

                ;/////////////////////////
                
;        ELSE
;                ld      a,(SCRMOD)
;                cp      4
;                jr      nc,ldirvm_new
;                call    setwrt
;                jr      ldirvm_cont
;ldirvm_new:
;                call    nsetwr
;ldirvm_cont:
;        ENDIF
                ex      de,hl
                dec     bc
                inc     c
                ld      a,b
                ld      b,c
                inc     a
                ld      c,VDP_DATA
myldirvm_lp:
                ;otir	;beware ... every outi takes 23 t-states , but every in or out access on MSX1 VDP must take 29 t-states apart
                outi					; this two lines
                jp		nz,myldirvm_lp	; will make the work
                dec     a
                jr      nz,myldirvm_lp
 
                ex      de,hl
                ret
	
	

scrolltext_load_font:

	/*
	ld		hl,font_buffer
	ld		de,CHRTBL+(128*8)						;font starts at 128=A 129=blank 130=B ...
	ld		bc,52*8									;416 bytes
	call	myldirvm;0x005c
	*/
	ld		hl,font_buffer
	ld		de,CHRTBL+(128*8) + 256*8						;font starts at 128=A 129=blank 130=B ...
	ld		bc,56*8	
	call	myldirvm;0x005c

	ld		hl,font_buffer
	ld		de,CHRTBL+(128*8)+ (256*8)*2						;font starts at 128=A 129=blank 130=B ...
	ld		bc,56*8	
	call	myldirvm;0x005c

	ret
	
	
	
	
scrolltext_dump_lines:

	ld		hl,text_buffer ;+ (8*32)
	ld		de,NAMTBL + (8*32)
	ld		bc,32*14 			;448 bytes
	
	call	myldirvm;0x005c

	ret



scrolltext_scroll_lines:
	ld		b,7 ;11
	ld		a,[odd]
	and		a				;a==0 (is even?)
	jr		nz,scroll_lines_odd
	;even
	;2->0, 4->2 ,...
	ld		a,1
	ld		[odd],a
	
	ld		de,text_buffer
	ld		hl,text_buffer+(32*2)
	
	jr		scroll_lines_loop
	
scroll_lines_odd:
	push	bc
	call	feed_next_two_lines
	pop		bc

	xor		a
	ld		[odd],a

	ld		de,text_buffer+32
	ld		hl,text_buffer+(32*3)

	
scroll_lines_loop:	
	push	bc	
	ld		bc,32
	ldir
	ld		bc,32
	add		hl,bc
	ex		de,hl
	add		hl,bc
	ex		de,hl
	pop		bc	
	djnz	scroll_lines_loop
	ret



feed_next_two_lines:
	ld		hl,[txt_ptr]
	ld		de,text_buffer+512-(32*2);text_buffer+768-(32*2)
	ld		bc,32*2
	ldir
	
	;ld		hl,[txt_ptr]
	;ld		bc,32*2
	;add		hl,bc
	ld		[txt_ptr],hl							;save pointer for later use

	ret




scroll_font_1_line:
	ld		hl,font_buffer+1
	ld		de,font_buffer
	ld		b,28
scroll_font_1_line_loop:	
	push	bc
	ld		bc,15
	ld		a,[de]
	ldir	
	ld		[de],a
	inc		hl
	inc		de
	pop		bc
	djnz	scroll_font_1_line_loop
	
	ret



scroll_text_load_ASCII:
	;first load game graphics to retrieve font
	
	ld		hl,scroll_text_load_ASCII_after_loading_patterns
	push	hl								;ret value

	call	activate_rom_page3

	ld		bc,scroll_text_load_ASCII_ret_address_1
	ld		hl,battle_patterns
	ld		de,CHRTBL
	jp		unpack_rle_2_vram

scroll_text_load_ASCII_ret_address_1:
	jp		restore_ram_page3	
	
	
scroll_text_load_ASCII_after_loading_patterns:	
	
	xor		a
	ld		hl,CHRTBL+(62*8) + (256*8)
	ld		bc,16
	call	0x0056

	xor		a
	ld		hl,CHRTBL+(62*8) + (256*8)*2
	ld		bc,16
	call	0x0056


	ld		hl,CHRTBL+(65*8) + (256*8)
	ld		de,text_buffer+500   ;29*8*2 < 500
	ld		bc,26*8
	call	0x0059
	
	

	;graphics buffer holds unpacked game tiles. From graphics_buffer + (43*8)  to 22+26 (48) * 8
		
	;leave the copy (with blank lines) on text_buffer (set warning ... it's being used)

	ld		a,26
	ld		hl,txtscrolllin					;holds counter (up to 48)
	ld		[hl],a
	
	ld		hl,text_buffer+500  ;second bank contains the complete font set
	push	hl
	ld		hl,text_buffer
	push	hl

scroll_text_load_ASCII_loop:	
	
	pop		hl								;txtbuffer (dest) next position
	ld		d,h
	ld		e,l
	inc		de								

	xor		a
	ld		[hl],a							;[hl=next dest pos]==0 , hl src, de(=hl+1) dest
	ld		bc,7
	ldir									;while line

	pop		hl								;hl current font character
	;de already set
	ld		bc,8
	ldir									;copy font caracter

	push	hl								;save next font caracter
	push	de								;save next dest post (text_fuffer)


	ld		hl,txtscrolllin
	dec		[hl]
	jr		nz,scroll_text_load_ASCII_loop

	pop		hl
	pop		de								;TODO: fix stack using sp reg
	
	;push	hl								;next dest pos
	
	xor		a
	ld		hl,text_buffer+(26*8*2)
	ld		[hl],a
	ld		de,text_buffer+(26*8*2)+1
	ld		bc,7
	ldir
	
	ld		hl,CHRTBL+(44*8) + (256*8)
	ld		de,text_buffer+(26*8*2)+8
	ld		bc,8
	call	0x0059
	
	xor		a
	ld		hl,text_buffer+(26*8*2)+16
	ld		[hl],a
	ld		de,text_buffer+(26*8*2)+17
	ld		bc,7
	ldir
	
	ld		hl,CHRTBL+(46*8)  + (256*8)
	ld		de,text_buffer+(26*8*2)+16+8
	ld		bc,8
	call	0x0059
	
	
	ret



TEXT:
	;incbin	"binary_data/story_txt.bin.pletter"
	incbin	"binary_data/final_text.bin.pletter"
	
